
package peerToPeerFacebook.proj;

import java.io.Serializable;

/**
 * This class creates a new Facebook user. It will create three files for the
 * new user Friends file, messages and pendingFriendRequest file
 * 
 */
public class CreateNewFacebookUser
{
    public static final String FriendFileSuffix = "_friends.txt";
    public static final String PendingFriendRequestsFileSuffix = "_pendingFriendRequests.txt";
    public static final String MessagesFileSuffix = "_messages.txt";
    private String format = "***************";
    protected RemoteInvoker client = null;
    protected String userName = null;
    protected IResultCallback result = null;

    public CreateNewFacebookUser(RemoteInvoker client, String userName, FBNode node)
    {
        this.client = client;
        this.userName = userName;
    }

    /**
     * helper method for printing the result of the failed RPC call
     */

    private void printErrorMsg()
    {
        System.out.println(" \n\n" + format + " The requested operation failed " + format +
                           "\n\n");
    }

      protected void start()
    {
        client.invoke(IServerFileIO.CreateFileMethodName,
                         new Serializable[] {userName + FriendFileSuffix},
                         new IResultCallback()
                         {
                             @Override
                             public void resultCallback(Status status, Serializable result)
                             {
                                 if (status == Status.Success)
                                     createMessageFile();
                                 else
                                      printErrorMsg();
                             }
                         });

    }

    protected void createMessageFile()
    {
        client.invoke(IServerFileIO.CreateFileMethodName,
                         new Serializable[] {userName + MessagesFileSuffix},
                         new IResultCallback()
                         {
                             @Override
                             public void resultCallback(Status status, Serializable result)
                             {
                                 if (status == Status.Success)
                                     createPendingFriendRequestFile();
                                 else
                                     printErrorMsg();
                             }
                         });
    }

    protected void createPendingFriendRequestFile()
    {
        client.invoke(IServerFileIO.CreateFileMethodName,
                         new Serializable[] {userName + PendingFriendRequestsFileSuffix},
                         new IResultCallback()
                         {
                             @Override
                             public void resultCallback(Status status, Serializable result)
                             {
                                 if (status == Status.Success)
                                 {
                                     System.out.println(" \n\n" + format +
                                                        "  Facebook account for " + userName +
                                                        " got created successfully " + format +
                                                        " \n\n ");

                                 }
                                 else
                                 {
                                     System.out.println(" \n\n" + format +
                                                        "  Facebook account creation for " +
                                                        userName + " failed" + format +
                                                        " \n\n ");
                                 }
                             }
                         });
    }

    /*
     * protected boolean success(Status status, Serializable result) { if
     * (status != Status.Success) this.result.resultCallback(status, result);
     * 
     * return status != Status.Success; }
     */
}
